package com.hmall.trade.listener;

import com.hmall.api.client.TradeClient;
import com.hmall.trade.domain.po.Order;
import com.hmall.trade.service.IOrderService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.context.annotation.Configuration;

@Slf4j
@Configuration
@RequiredArgsConstructor
public class PayStatusListener {
    private final TradeClient tradeClient;
    private final IOrderService iOrderService;
    // 通过rabbitMq通知对应的微服务更新订单状态
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name="trade.pay.success.queue"),
            exchange = @Exchange(name = "pay.direct",type = ExchangeTypes.DIRECT),
            key = "pay.success"
    ))
    public void listenPaySuccess(Long id){
        log.info("id:{}",id);
        // 查询订单
        Order order = iOrderService.getById(id);
        // 判断订单状态是否不为未支付
        if(order==null || order.getStatus()!=1){
            // 不为未支付，说明订单已处理，不做操作
            return;
        }
        tradeClient.markOrderPaySuccess(id);
    }
}
